﻿using Common;
using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZoneLine.Views
{
    /// <summary>
    /// PackageReplaceView.xaml 的交互逻辑
    /// </summary>
    public partial class PackageReplaceView : Window
    {
        private static PackageReplaceView _view;

        private static object _obj = new object();

        public static PackageReplaceView GetView()
        {
            lock (_obj)
            {
                if (_view == null)
                {
                    _view = new PackageReplaceView();
                }
            }
            return _view;
        }

        private PackageReplaceView()
        {
            InitializeComponent();
            this.Loaded += PackageReplaceView_Loaded;
            this.Closed += PackageReplaceView_Closed;
            Cache.EnterReplaceStatus = true;
        }

        private void PackageReplaceView_Loaded(object sender, RoutedEventArgs e)
        {
            oldCode.Focus();

            MessageCenter.Subscribe(nameof(PackageReplaceView), nameof(PackageReplaceView), (ss) =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    newCode.Focus();
                });

                return Task.CompletedTask;
            });
        }

        private void PackageReplaceView_Closed(object sender, EventArgs e)
        {
            _view = null;
            Cache.EnterReplaceStatus = false;

            MessageCenter.Unsubscribe(nameof(PackageReplaceView), nameof(PackageReplaceView));
        }
    }
}
